// pages/search/index.js
import regeneratorRuntime, { async } from '../../lib/runtime/runtime'

import { request } from "../../request/index"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods: [],
    isFocus: false,
    inputValue: ""
  },
  TimeId: 0,
  // 输入框的值改变就会触发的事件
  handleInput(e) {
    // console.log(e);
    const { value } = e.detail;
    if (!value.trim()) {
      this.setData({
        goods: [],
        isFocus: false
      })
      return;
    }
    clearTimeout(this.TimeId);
    this.TimeId = setTimeout(() => {
      this.qsearch(value);
    }, 1000);
    this.setData({
      isFocus: true
    })
  },
  async qsearch(query) {
    const res = await request({ url: "/goods/qsearch", data: { query } });
    console.log(res);
    this.setData({
      goods: res.data.message
    })
  },
  handleCancel() {
    this.setData({
      inputValue: "",
      isFocus: false,
      goods: []
    })
  }
})